﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinformPrint
{
    public partial class Form1 : Form
    {
        int ii = 0;
        public Form1()
        {
            InitializeComponent();

            this.printDocument1.OriginAtMargins = true;//启用页边距
            this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

            Timer timer1 = new Timer();
            timer1.Interval = 5000;
            timer1.Enabled = true;
            timer1.Tick += new EventHandler(timer1EventProcessor);//添加事件
        }

        //打印设置
        private void button1_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.Document = printDocument1;
            this.pageSetupDialog1.ShowDialog(); 
        }
        //打印预览
        private void button2_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.ShowDialog(); 
        }
        //打印
        private void button3_Click(object sender, EventArgs e)
        {
            this.printDialog1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
            
            this.printDocument1.Print();
        }

        //打印内容的设置
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //打印内容 为 整个Form
            //Image myFormImage;
            //myFormImage = new Bitmap(this.Width, this.Height);
            //Graphics g = Graphics.FromImage(myFormImage);
            //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
            //e.Graphics.DrawImage(myFormImage, 0, 0);

            ////打印内容 为 局部的 this.panel1
            this.label2.Text = "ST-DD00446420171204184748";
            Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
            panel1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);

            //打印内容 为 自定义文本内容 
            //Font font = new Font("宋体", 20);
            //Brush bru = Brushes.Blue;
            //for (int i = 1; i <= 5; i++)
            //{
            //    e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
            //}
            //e.HasMorePages = true;
            //for (int i = 1; i <= 8; i++)
            //{
            //    e.Graphics.DrawString("wellcome ", font, bru, i * 20, i * 20);
            //}
            //e.HasMorePages = false;

            //分页
            //if (ii < 5) //如果将 if 改成 for 语句, 会将几页张合在一张纸上, 而达不到要求
            //{
            //    Font font = new Font("宋休", 12 + 30 * ii);
            //    e.Graphics.DrawString(ii.ToString(), font, Brushes.Black, new Point(10, 10));
            //    ii++;
            //}
            //if (ii < 4) //以此数 4 为准而不是上面的5，即出现4次
            //{
            //    e.HasMorePages = true;
            //}
            //else if (ii == 4)
            //{
            //    e.HasMorePages = false;
            //}
        }

        private void timer1EventProcessor(object source, EventArgs e)
        {
            //this.printDocument1.Print();
            //this.label1.Text = DateTime.Now.ToString();
        }

        

    }
}
